﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Wp.DAL;

namespace Wp.BLL
{
    /// <summary>
    /// Summary description for TblComments
    /// </summary>
    public class TblCommentsManagerBase
    {
        public TblCommentsManagerBase()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public void InsertUpdate(TblCommentsEntity item)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", item.Id));
            ls.Add(new SqlParameter("@PostId", item.PostId));
            ls.Add(new SqlParameter("@Author", item.Author));
            ls.Add(new SqlParameter("@Email", item.Email));
            ls.Add(new SqlParameter("@IP", item.IP));
            ls.Add(new SqlParameter("@Content", item.Content));
            ls.Add(new SqlParameter("@ParentId", item.ParentId));
            ls.Add(new SqlParameter("@CreatedDate", item.CreatedDate));
            ls.Add(new SqlParameter("@Approved", item.Approved));
            ls.Add(new SqlParameter("@CreatedBy", item.CreatedBy));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblComment", sqlparam);
        }
        public void InsertUpdate(int Id, int PostId, string Author, string Email, string IP, string Content, int ParentId, System.DateTime CreatedDate, string Approved, string CreatedBy)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@PostId", PostId));
            ls.Add(new SqlParameter("@Author", Author));
            ls.Add(new SqlParameter("@Email", Email));
            ls.Add(new SqlParameter("@IP", IP));
            ls.Add(new SqlParameter("@Content", Content));
            ls.Add(new SqlParameter("@ParentId", ParentId));
            ls.Add(new SqlParameter("@CreatedDate", CreatedDate));
            ls.Add(new SqlParameter("@Approved", Approved));
            ls.Add(new SqlParameter("@CreatedBy", CreatedBy));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblComment", sqlparam);
        }
        public void Delete(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_DeleteTblComment", sqlparam);
        }
        public TblCommentsEntity GetTblCommentsEntity(DataRow dr)
        {
            TblCommentsEntity item = new TblCommentsEntity();            
            if (dr.Table.Columns.Contains("Id"))
                item.Id = dr["Id"].ToInt();              
            if (dr.Table.Columns.Contains("PostId"))
                item.PostId = dr["PostId"].ToInt();              
            if (dr.Table.Columns.Contains("Author"))
                item.Author = dr["Author"].ToString();              
            if (dr.Table.Columns.Contains("Email"))
                item.Email = dr["Email"].ToString();              
            if (dr.Table.Columns.Contains("IP"))
                item.IP = dr["IP"].ToString();              
            if (dr.Table.Columns.Contains("Content"))
                item.Content = dr["Content"].ToString();              
            if (dr.Table.Columns.Contains("ParentId"))
                item.ParentId = dr["ParentId"].ToInt();              
            if (dr.Table.Columns.Contains("CreatedDate"))
                item.CreatedDate = dr["CreatedDate"].ToDateTime();              
            if (dr.Table.Columns.Contains("Approved"))
                item.Approved = dr["Approved"].ToString();              
            if (dr.Table.Columns.Contains("CreatedBy"))
                item.CreatedBy = dr["CreatedBy"].ToString();
            if (dr.Table.Columns.Contains("PostTitle"))
                item.PostTitle = dr["PostTitle"].ToString();
            if (dr.Table.Columns.Contains("TextId"))
                item.TextId = dr["TextId"].ToString();   
            return item;
        }        
        public DataTable SelectOneRDT(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblComment", sqlparam);
        }
        public TblCommentsEntity SelectOne(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblComment", sqlparam);
            if(dt!=null && dt.Rows.Count>0)
                return GetTblCommentsEntity(dt.Rows[0]);
            return null;
        }
        public DataTable SelectAllRDT()
        {
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblCommentsAll", null);
        }
        public List<TblCommentsEntity> SelectAll()
        {
            List<TblCommentsEntity> lsresult= new List<TblCommentsEntity>();
            DataTable dt =  SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblCommentsAll", null);
            if (dt != null && dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(GetTblCommentsEntity(dt.Rows[i]));            
            return lsresult;
        }
    }
}

